home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_10.lha / 7_10 / test4.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  3KB  |  93 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. * based on cfront 2.0 b5 demo program triv.C */
  6. include <debug.h>    /* DELETE */
  7. *ident    "@(#)ctrans:demo/task/triv.C    1.1" */
  8. **************************************************************************
  9.         Copyright (c) 1984 AT&T
  10.             All Rights Reserved      
  11.  
  12. THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  13.  
  14. The copyright notice above does not evidence any       
  15. actual or intended publication of such source code.
  16.  
  17. ****************************************************************************/
  18. include <process.h>
  19.  
  20. * trivial process example:
  21.        Make a set of processes which pass an object round between themselves.
  22. Each process gets an object from the head of one queue, and puts
  23. the object on the tail of another queue.
  24. Main creates each process, then puts the object on a queue.
  25. Each process quits after getting the object MAX_CYCLES times.
  26. /
  27.  
  28. onst int NTASKS = 1;
  29. onst int MAX_CYCLES = 5;
  30.  
  31. truct pc : process {                // derive a class from process
  32. queue_tail *t;
  33. queue_head *h;
  34. char *n;
  35.        pc(char*nn, queue_tail*nt, queue_head*nh)    // process is not intended to be used directly
  36. : (nn)
  37.            if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! new pc(" << nn << ")\n";
  38.            cout << "new pc(" << nn << ")\n";
  39.     t = nt; h = nh; n = nn;
  40. }
  41. long main();
  42. ;                        
  43.  
  44. ong pc::main()    // process body serves as
  45.         // "main" program for process
  46.  
  47.        for (int i = 0; i < MAX_CYCLES; i++) {
  48.                process_object* p = h->get();
  49.                if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! process " << n << "\n";
  50.                cout << "process " << n << "\n";
  51.                t->put(p);
  52.        }
  53. if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! process " << n << ": done.\n";
  54. cout << "process " << n << ": done.\n";
  55. return 0;        // Always end process constructors with return.
  56.  
  57.  
  58. ain()
  59.  
  60.        queue_head* hh = new queue_head;
  61.        queue_tail* t = hh->tail();        // hh and t refer to same queue.
  62.        queue_head* h;
  63.  
  64.        if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main\n";
  65.        cout << "main\n";
  66.  
  67.        for (int i=0; i<NTASKS; i++) {
  68.                char* n = new char[2];    // make a one letter process name
  69.                n[0] = 'a'+i;
  70.                n[1] = 0;
  71.  
  72.                h = new queue_head;
  73.                (new pc(n,t,h))->exec(); // create a new process and start execution
  74.                if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main()'s loop\n";
  75.                cout << "main()'s loop\n";
  76.                t = h->tail();
  77.        }
  78.  
  79.        (new pc("first pc",t,hh))->exec();    // create another new process
  80.        if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main: here we go\n";
  81.        cout << "main: here we go\n";
  82.        t->put(new process_object);        // put the object on a queue
  83.        if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main: exit\n";
  84.        cout << "main: exit\n";
  85. main_process()->exit(0);        // main is a process too; it must also
  86.                 // end with exit (to allow any
  87.                 // remaining processes to run, otherwise
  88.                 // the whole process would exit).
  89.    printf("should not get here\n");
  90.    return 0;
  91.  
  92.